package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import dto.LogonDTO;

public class LogonPage extends JDialog {

	private static final long serialVersionUID = 1L;

	private JLabel labelErrorMessage;
	private JPanel contentPanel;
	private JTextField txtUsername;
	private JPasswordField txtPassword;

	private JButton btnLogonButton = new JButton("Enter");
	private JButton btnClearButton = new JButton("Clear");

	private LogonDTO dto;

	public LogonDTO getDto() {

		dto.setUserName(txtUsername.getText().trim());
		dto.setPassword(txtPassword.getPassword());
		return dto;
	}

	/**
	 * Create the dialog.
	 */
	public LogonPage() {

		dto = new LogonDTO();

		contentPanel = new JPanel();
		txtUsername = new JTextField();

		setBounds(100, 100, 450, 331);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		contentPanel.setLayout(new MigLayout("", "[grow][]",
				"[][][][][][][][][][][][][][][][][]"));

		JLabel lblWelcomToJ = new JLabel("Welcome to J&A Library!");
		lblWelcomToJ.setFont(new Font("Arial", Font.BOLD, 16));
		contentPanel.add(lblWelcomToJ, "cell 0 0");

		JLabel lblUsername = new JLabel("Username:");
		contentPanel.add(lblUsername, "cell 0 2");

		txtUsername.setText("GUEST");
		contentPanel.add(txtUsername, "cell 0 3 2 1,growx");
		txtUsername.setColumns(10);

		JLabel lbluseGuestIf = new JLabel("*use GUEST if non-member");
		lbluseGuestIf.setFont(new Font("Arial", Font.ITALIC, 11));
		contentPanel.add(lbluseGuestIf, "cell 0 4");

		JLabel lblPassword = new JLabel("Password:");
		contentPanel.add(lblPassword, "cell 0 6");

		txtPassword = new JPasswordField();
		contentPanel.add(txtPassword, "cell 0 8,growx,aligny top");

		JLabel lblleaveBlankIf = new JLabel("*leave blank if GUEST");
		lblleaveBlankIf.setFont(new Font("Arial", Font.ITALIC, 11));
		contentPanel.add(lblleaveBlankIf, "cell 0 9");

		// ENTER button
		contentPanel.add(btnLogonButton, "cell 0 11");

		InputMap imLogon = btnLogonButton.getInputMap();
		imLogon.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
		imLogon.put(KeyStroke.getKeyStroke("released ENTER"), "released");

		// CLEAR button
		contentPanel.add(btnClearButton, "cell 1 11");
		InputMap imClear = btnClearButton.getInputMap();
		imClear.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
		imClear.put(KeyStroke.getKeyStroke("released ENTER"), "released");

		labelErrorMessage = new JLabel("  ");
		labelErrorMessage.setForeground(Color.RED);
		labelErrorMessage.setFont(new Font("Arial", Font.BOLD, 12));
		contentPanel.add(labelErrorMessage, "cell 0 13");

		JLabel lblNoteGuestsWill = new JLabel(
				"NOTE: GUESTs will be able to search for books,");
		lblNoteGuestsWill.setFont(new Font("Arial", Font.ITALIC, 11));
		contentPanel.add(lblNoteGuestsWill, "cell 0 15");

		JLabel lblButOnlyMembers = new JLabel(
				"but only Members may borrow them.");
		lblButOnlyMembers.setFont(new Font("Arial", Font.ITALIC, 11));
		contentPanel.add(lblButOnlyMembers, "cell 0 16");

	}

	/**
	 * Action done when 'ENTER' button is pressed.
	 * 
	 * @param al
	 */
	public void addLogonListener(ActionListener al) {
		btnLogonButton.addActionListener(al);
	}

	/**
	 * Action done when 'CLEAR' button is pressed.
	 */
	public void addClearListener() {

		btnClearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtUsername.setText("GUEST");
				txtPassword.setText("");
				labelErrorMessage.setText("");
			}
		});

		JButton button = new JButton("Button");
		InputMap im = button.getInputMap();
		im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
		im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
	}

	public void displayError(String errorMsg) {
		labelErrorMessage.setText(errorMsg);
	}
}
